package org.ns.learn.rpc.codec;

import com.alibaba.fastjson.JSON;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

import java.nio.charset.Charset;

public class RpcMessageRequestEncode extends MessageToByteEncoder<RpcRequest> {
    @Override
    protected void encode(ChannelHandlerContext ctx, RpcRequest msg, ByteBuf out) throws Exception {
        String json = JSON.toJSONString(msg);
        byte[] bytes = json.getBytes(Charset.forName("UTF-8"));
        int length = bytes.length;
        out.writeInt(length);
        out.writeBytes(bytes);
    }
}
